// pages/goodsManagement/goodsManagementIndex.js
const controllers = require('../../controllers/controllerIndex.js').controller;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: "",
    type: "",
    rightList: [],
    jsonInfo: {},
    lastPage: false,
    pageNumber: 1,
    totalRow: 0,
    isHiddenManage: false,
    keyword: '',
    timer: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const id = options.id
    const type = options.type
    this.setData({
      id,
      type
    })
    this.getProduct()
  },




  // 批量处理商品
  setBatchGoods(e) {
    const that = this
    var state = e.currentTarget.dataset.state;
    let goodsIds = []
    this.data.rightList.forEach(item => {
      if (item.selset) {
        goodsIds.push(item.id)
      }
    })
    if (goodsIds.length == 0) {
      wx.showToast({
        title: '请选择商品',
        icon: "none"
      })
      return
    }
    goodsIds = goodsIds.join(",")
    if (this.data.type == "add") {
      wx.showModal({
        title: '添加',
        content: '是否添加',
        success(res) {
          if (res.confirm) {
            that.BatchAdd(goodsIds)
          }
        }
      })
    } else {
      wx.showModal({
        title: '移除',
        content: '是否移除',
        success(res) {
          if (res.confirm) {
            that.BatchRemove(goodsIds)
          }
        }
      })
    }

  },


  //批量移除
  BatchRemove(goodsIds) {
    controllers.batchDelGoodsLabel({
      sessionId: wx.getStorageSync('token'),
      shopId: wx.getStorageSync('shopId'),
      labelId: this.data.id,
      goodsIds,
    }).then(res => {
      wx.showToast({
        title: '移除成功',
        icon: 'none',
        duration: 2000
      })
      setTimeout(() => {
        this.cancel()
      }, 1000)
    })
  },

  //批量添加
  BatchAdd(goodsIds) {
    controllers.batchSetGoodsLabel({
      sessionId: wx.getStorageSync('token'),
      shopId: wx.getStorageSync('shopId'),
      labelId: this.data.id,
      goodsIds,
    }).then(res => {
      wx.showToast({
        title: '添加成功',
        icon: 'none',
        duration: 2000
      })
      setTimeout(() => {
        this.cancel()
      }, 1000)
    })
  },

  // navchoice(e) {
  //   const id = e.currentTarget.id
  //   this.setData({
  //     id,
  //     pageNumber: 1,
  //     scrollTop: 0,
  //   })
  //   this.getProduct(id);
  // },


  // 未分类商品列表
  async getProduct() {
    var page = this.data.pageNumber
    var that = this
    var jsonInfo = {
      pageSize: 20,
      pageNumber: page,
      keyword: this.data.keyword
    }
    jsonInfo.shopId = wx.getStorageSync('shopId')
    let res = {}
    if (this.data.type == "remove") {
      jsonInfo.labelId = this.data.id
      res = await controllers.shopLabelGoodsPage(jsonInfo)
    } else {
      jsonInfo.type = 1
      res = await controllers.noLabelGoodsPage(jsonInfo)
    }
    let rightList = that.data.rightList
    rightList = that.data.rightList.concat(res.data.list)

    that.setData({
      rightList: rightList,
      jsonInfo: jsonInfo,
      lastPage: res.data.lastPage,
      totalRow: res.data.totalRow
    })
  },
  lower(e) {
    if (!this.data.lastPage) {
      this.setData({
        pageNumber:this.data.pageNumber + 1
      })
      this.getProduct()
    }
  },

  // 搜索
  goodsSearch(e) {
    const keyword = e.detail.value
    
    if (this.data.timer) clearTimeout(this.data.timer)
    this.data.timer = setTimeout(() => {
      this.setData({
        keyword,
        pageNumber:1,
        rightList:[]
      })
      this.getProduct()
    }, 500)
  },

  // 取消
  cancel() {
    wx.navigateBack({
      delta: 0,
    })
  },


})